# -*- coding: utf-8 -*-

from datetime import datetime
from odoo import models, fields, api


class OpenItemsReportWizard(models.TransientModel):
    """Open items report wizard."""

    _name = "invoice.items.report.wizard"
    _description = "Invoice Items Report Wizard"

    company_id = fields.Many2one(
        comodel_name='res.company',
        default=lambda self: self.env.user.company_id,
        string='Company'
    )
    invoice_ids = fields.Many2many(
        comodel_name='account.invoice',
        string='Filter Invoice',
        domain="[('type', 'in', ('out_invoice', 'out_refund')),('state', '=', 'open')]"
    )

    invoice_line_ids = fields.Many2many(
        comodel_name='account.invoice.line',
        string='Filter Invoice',
        domain="[('type', 'in', ('out_invoice', 'out_refund')),('state', '=', 'open'), ('is_export_xls', '=', False)]"
    )

    @api.multi
    def button_export_customer_xlsx(self):
        self.ensure_one()
        return self._customer_export(xlsx_report=True)

    @api.multi
    def button_export_xlsx(self):
        self.ensure_one()
        return self._export(xlsx_report=True)

    def _prepare_report_invoice_items(self):
        self.ensure_one()
        return {
            'filter_invoice_line_ids': [(6, 0, self.invoice_line_ids.ids)],
        }

    def _export(self, xlsx_report=False):
        model = self.env['report.invoice.xls']
        report = model.create(self._prepare_report_invoice_items())
        return report.print_report(xlsx_report)

    def _customer_export(self, xlsx_report=False):
        model = self.env['report.invoice.xls']
        report = model.create(self._prepare_report_invoice_items())
        return report.print_customer_report(xlsx_report)
